BREAKING(cache): add unified Cache#7082
Open
tomas-zijdemans wants to merge 5 commits intodenoland:mainfrom
Open
BREAKING(cache): add unified Cache#7082tomas-zijdemans wants to merge 5 commits intodenoland:mainfrom
Cache#7082tomas-zijdemans wants to merge 5 commits intodenoland:mainfrom
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7082 +/- ##
==========================================
+ Coverage 94.61% 94.64% +0.02%
==========================================
Files 634 633 -1
Lines 51799 52059 +260
Branches 9329 9393 +64
==========================================
+ Hits 49009 49269 +260
Misses 2216 2216
Partials 574 574 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Member
|
I'll hold on reviewing this one until #7076 lands. |
CacheCache
bartlomieju
requested changes
Apr 21, 2026
Member
bartlomieju
left a comment
There was a problem hiding this comment.
This needs a rebase — there are conflicts with main that need resolving.
Two inline comments below.
Introduces a single `Cache` class that subsumes both `LruCache` and `TtlCache` into a configuration-driven API (maxSize, ttl, sliding expiration, stale-while-revalidate refresh, and onRemove). The new implementation delegates expiration tracking to `IndexedHeap` from `@std/data-structures/unstable-indexed-heap` for O(log n) evictions. - Add cache/cache.ts and cache/cache_test.ts - Remove cache/lru_cache.ts, cache/ttl_cache.ts and their tests - Update cache/memoize.ts doc to reference Cache - Update cache/mod.ts and cache/deno.json exports Made-with: Cursor
Contributor
Author
|
Thanks,
|
Contributor
Author
|
Renamed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR replaces
LruCacheandTtlCachewith a singleCache<K, V>that unifies LRU eviction, TTL expiration, stale-while-revalidate, and load-through (getOrLoad).The existing caches extend
Map, so inherited methods bypass LRU/TTL logic entirely. Both subclassMap, making it structurally impossible to compose LRU with TTL.Cache<K, V>uses composition over inheritance: it owns aMapfor storage and delegates toIndexedHeapfor deadline-ordered expiration. Mode is determined by options, not class choice, and a discriminated union onCacheOptionsmakes illegal combinations compile-time errors.Beyond fixing the structural issues, this introduces capabilities that didn't exist before:
CacheLike, a more natural type namegetOrLoad(key, loader)with automatic in-flight deduplicationsetTimeoutsSymbol.disposeSince
@std/cacheis still experimental (0.2.2), this is the right time to make a clean break before stabilization locks things in.Bonus: The new
Cachehas significant performance improvements for real-life workflows (e.g. in my testing, write-heavy workloads (set, eviction) are 4-22x faster. hot-key reads are 55x faster thanks to a linked-list)